package com.liuhouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liuhouse.constants.SystemCanstants;
import com.liuhouse.pojo.Article;
import com.liuhouse.pojo.Category;
import com.liuhouse.mapper.CategoryMapper;
import com.liuhouse.service.IArticleService;
import com.liuhouse.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuhouse.utils.BeanCopyUtils;
import com.liuhouse.utils.ResponseResult;
import com.liuhouse.vo.CategoryVO;
import com.liuhouse.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * <p>
 * 分类表 服务实现类
 * </p>
 *
 * @author liuhouse
 * @since 2024-01-27
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Autowired
    private IArticleService articleService;

    @Override
    //查询分类列表的核心代码
    public ResponseResult getCategoryList() {
        //1.查询出当前所有已经发布的文章列表
        //也就是查询当前状态是0的文章  至于 del_flag 是否删除 mybatisPlus已经设置好了逻辑删除的字段
        LambdaQueryWrapper<Article> articleWrapper = new LambdaQueryWrapper<>();
        articleWrapper.eq(Article::getStatus , SystemCanstants.ARTICLE_STATUS_NORMAL);
        List<Article> articleList = articleService.list(articleWrapper);
        //2.从列表中筛选出文章分类
        //获取文章分类的id,并且去重,使用stream流来处理上一行得到的文章列表集合
        Set<Long> categoryIds = articleList.stream()
                .map(article -> article.getCategoryId())
                .collect(Collectors.toSet());//把文章分类的id转换成set集合【set集合是可以直接去重的】
        //3.根据分类ids,去查询分类列表
        List<Category> categories = this.listByIds(categoryIds);
        //对分类进行筛选,要的是状态是正常的分类 也就是为0
        //注意SystemCanstants是我们写的一个常量类,用来解决字面值的书写问题
        categories = categories.stream()
                .filter(category -> SystemCanstants.STATUS_NORMAL.equals(category.getStatus()))
                .collect(Collectors.toList());
        //4.进行vo转换 ， 然后返回数据
        List<CategoryVO> categoryVOS = BeanCopyUtils.copyBeanList(categories, CategoryVO.class);
        //返回最终要返回的数据
        return ResponseResult.okResult(categoryVOS);
    }

    //查询文章分类的接口
    @Override
    public List<CategoryVO> listAllCategory() {
        //1.查询所有的文章分类
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getStatus , SystemCanstants.NORMAL);
        List<Category> list = list(queryWrapper);
        //2.进行转化返回
        List<CategoryVO> categoryVOS = BeanCopyUtils.copyBeanList(list, CategoryVO.class);
        return categoryVOS;
    }


    //分页查询分类列表
    @Override
    public PageVo selectCategoryPage(Category category, Integer pageNum, Integer pageSize) {
        //1.构建查询条件
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //如果分类名称存在就模糊查询分类名称
        queryWrapper.like(StringUtils.hasText(category.getName()) , Category::getName , category.getName());
        //如果状态存在不为null那么就进行状态的查询
        queryWrapper.eq(Objects.nonNull(category.getStatus()) , Category::getStatus , category.getStatus());
        //2.构建分页数据
        Page<Category> page = new Page<>(pageNum , pageSize);
        //进行数据查询
        page(page , queryWrapper);
        //3.进行数据返回
        PageVo pageVo = new PageVo();
        pageVo.setRows(page.getRecords());
        pageVo.setTotal(page.getTotal());
        return pageVo;
    }


    //添加分类
    @Override
    public ResponseResult addCategory(CategoryVO categoryVO) {
       //1.将传递过来的数据进行转换
        Category category = BeanCopyUtils.copyBean(categoryVO, Category.class);
        //2.加入到数据库
        this.save(category);
        return ResponseResult.okResult();
    }
}
